package com.Ruier.controller;

import com.Ruier.pojo.Account;
import com.Ruier.pojo.Result;
import com.Ruier.service.AccountService;
import com.Ruier.utils.Uploadfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/accounts")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private Uploadfile uploadfile;
    @GetMapping("/checkUserName")
    public Result checkUserName(String userName){
        boolean userNameIsNULL = accountService.checkUserName(userName);
        if(userNameIsNULL){
            return Result.success();
        }else return Result.error("UserNameIsRepeat");
    }
    @PostMapping("/register")
    public Result register(@RequestBody Account account){
        accountService.addAccount(account);
        return Result.success();
    }
    @RequestMapping("/handleImage")
    public Result handleImage(MultipartFile file) throws Exception {
        String url = uploadfile.upload(file);
        return Result.success(url);
    }
    @PostMapping("/login")
    public Result login(@RequestBody Account account){
        boolean loginsuccess = accountService.login(account);
        if (loginsuccess){
            return Result.success();
        }else return Result.error("login_error");
    }
}
